/**
 *	@file	telseq_ookido.c
 *	@brief	オーキド　会話データ抽選＆再生
 *	@author	Miyuki Iwasawa
 *	@date	09.02.06
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/pm_rtc.h"
#include "gflib/touchpanel.h"
#include "field/eventflag.h"
#include "field/evwkdef.h"
#include "field/sysflag.h"
#include "fielddata/script/saveflag.h"
#include "itemtool/itemsym.h"
#include "msgdata/msg.naix"
#include "msgdata/msg_pg_tel.h"
#include "msgdata/msg_tel_p_ookido.h"

#include "savedata/zukanwork.h"
#include "../pm_tel.h"
#include "tel/pmtel_def.h"
#include "tel/pmtel_datman.h"
#include "tel/pmtel_dat_def.h"
#include "../pmtel_seq.h"
#include "telseq_play.h"

//field/scr_tool.hの中身全部をincludeできないため直接extern
extern u16 GetJohtoZukanHyoukaMsgID( u16 num ,u16 sex, u16* me_no);
extern u16 GetZenkokuZukanHyoukaMsgID( u16 num ,u16 sex, u16* me_no );


/**
 *	@brief	電話　オーキドの会話抽選
 */
u16 PMTelPlayL_Ookido(PMTEL_DATA_MAN* man,PMTEL_TALK_WORK* wk)
{
	ZUKAN_WORK* zukan;

	wk->seq_code = TEL_SEQ_SIMPLE;
	zukan = SaveData_GetZukanWork(man->save);

	//イベント着信
	if(wk->ev_type == TEL_RECEIVE_EVENT){
		wk->seq_code = TEL_SEQ_SIMPLE;
		return TEL_TK_OOKIDO_BADGE_ALL_GET;
	}

	//ノーマル着信
	if(wk->is_receive){
		u16 rank;
		rank = (ZukanWork_GetPokeGetCount(zukan)/TELEV_OOKIDO_ZUKAN_HYOUKA_INTERVAL);

		if(rank == 0){
			//この条件では呼ばれないはずだが一応セーフティをかけておく
			rank = 1;
		}else if(rank > TELEV_OOKIDO_ZUKAN_HYOUKA_RANK_MAX){
			rank = TELEV_OOKIDO_ZUKAN_HYOUKA_RANK_MAX;
		}
		return TEL_TK_OOKIDO_EV_OVER50+(rank-1);
	}

	//同一マップにいるか？
	if(wk->pbk->map_id == man->zone_id)	{
		return TEL_TK_OOKIDO_NE;
	}
	//ノーマル発信
	if(EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_ZENKOKU_ZUKAN_COMP)){
		//全国図鑑完成メッセージを既に聞いている
		return TEL_TK_OOKIDO_A_ZENKOKU_ZUKAN_COMP;
	}
	if(	!ZukanWork_GetZenkokuZukanFlag(zukan) &&
		EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_JOHTO_ZUKAN_COMP)){
		//全国図鑑非オープンで、ジョウト図鑑完成メッセージを既に聞いている
		return TEL_TK_OOKIDO_A_JOHTO_ZUKAN_COMP;
	}
	wk->seq_code = TEL_SEQ_OOKIDO_ZUKAN_HYOUKA;
	return 0;
}

/**
 *	@brief	オーキドの図鑑評価シーケンス
 */
int PMTelSeq_TypeOokidoZukanHyouka(PMTEL_DATA_MAN* man)
{
	PMTEL_TALK_WORK *wk = &man->twk;

	switch(wk->seq){
	case 0:
		//タグ展開処理
		PMTel_TagSpreadCommon(man);

		{
			ZUKAN_WORK* zukan_wk = SaveData_GetZukanWork(man->save);

			wk->tmp_flag1 = ZukanWork_GetZenkokuZukanFlag(zukan_wk);
			wk->tmp_flag2 = ZukanWork_CheckJohtoComp(zukan_wk);
			wk->tmp_flag3 = ZukanWork_CheckZenkokuComp(zukan_wk);
			wk->tmp_wk = (void*)zukan_wk;
		}
		//挨拶出力
		PMTel_MsgPrintGmmSex(man,man->m_person,m_ookido_greeting_m,m_ookido_greeting_f);
		break;
	case 1:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//どんな感じかな？
		PMTel_MsgPrintGmm(man,man->m_person,m_ookido_zukan_00);
		break;
	case 2:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		//選択肢表示
		if(wk->tmp_flag1){	//全国図鑑を手に入れていたら
			if(EventWork_CheckEventFlag(man->ev_sv,SYS_FLAG_JOHTO_ZUKAN_COMP)){
				wk->tmp_u8 = 2;	//ジョウト図鑑コンプリートメッセージを聞いた
			}else{
				wk->tmp_u8 = 1;	//ジョウト未完成
			}
		}else{
			wk->tmp_u8 = 0;	//ジョウトのみ
		}
		PMTel_SelBoxSet(man,SBOX_ID_ZUKAN01+wk->tmp_u8);
		break;
	case 3:
		//選択待ち
		{
			u16	see,get;
			u32 ret = PMTel_SelBoxMain(man);

			if(ret == SBOX_SELECT_NULL){
				return FALSE;
			}
			PMTel_SelBoxExit(man);

			if(ret == 0){
				if(wk->tmp_u8 == 2){	//全国図鑑のみ評価のとき
					wk->tmp_flag4 = TRUE;	//全国図鑑のみ評価の時は0番目が全国
				}else{
					wk->tmp_flag4 = FALSE;
				}
			}else if(ret == 1 && (wk->tmp_u8 == 1)){	//全国図鑑チェック
				wk->tmp_flag4 = TRUE;
			}else{	//やめる
				PMTel_MsgPrintGmm(man,man->m_person,m_ookido_zukan_06);
				wk->seq = 0xFF;
				return FALSE;
			}

			if(wk->tmp_flag4 == 0){	//ジョウト
				see = ZukanWork_GetJohtoPokeSeeCount((ZUKAN_WORK*)wk->tmp_wk);
				get = ZukanWork_GetJohtoPokeGetCount((ZUKAN_WORK*)wk->tmp_wk);
			}else{	//全国図鑑評価
				see = ZukanWork_GetPokeSeeCount((ZUKAN_WORK*)wk->tmp_wk);
				get = ZukanWork_GetPokeGetCount((ZUKAN_WORK*)wk->tmp_wk);
			}
			WORDSET_RegisterNumber( man->wset, 5,
				see, 3,	NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );
			WORDSET_RegisterNumber( man->wset, 6,
				get, 3,	NUMBER_DISPTYPE_LEFT, NUMBER_CODETYPE_DEFAULT );

			//見つけたポケモン…
			PMTel_MsgPrintGmm(man,man->m_person,m_ookido_zukan_05);
		}
		break;
	case 4:
		if(!PMTel_MsgPrintWait(man)) return FALSE;

		{
			u16 me_no,msg_no;
			u8	flag = 0;
			if(wk->tmp_flag4 == 0){	//ジョウト
				msg_no = GetJohtoZukanHyoukaMsgID(
					ZukanWork_GetJohtoGetCompCount( (ZUKAN_WORK*)wk->tmp_wk ),man->my_sex,&me_no );
				if(wk->tmp_flag2){	//ジョウト図鑑完成してたらフラグを立てる
					EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_JOHTO_ZUKAN_COMP);
					flag = 1;
				}
			}else{
				msg_no = GetZenkokuZukanHyoukaMsgID(
					ZukanWork_GetZenkokuGetCompCount( (ZUKAN_WORK*)wk->tmp_wk ),man->my_sex,&me_no );
				if(wk->tmp_flag3){	//全国図鑑完成してたらフラグを立てる
					EventWork_SetEventFlag(man->ev_sv,SYS_FLAG_ZENKOKU_ZUKAN_COMP);
					flag = 1;
				}
			}
			Snd_MePlay( me_no );
			PMTel_MsgPrintGmm(man,man->m_person,msg_no);

			if(flag){	//5は飛ばす
				wk->seq = 0xFF;
				return FALSE;
			}
		}
		break;
	case 5:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		if(Snd_MePlayCheckBgmPlay()) return FALSE;

		//また見せてくれたまえ
		PMTel_MsgPrintGmm(man,man->m_person,m_ookido_zukan_06);
		break;
	default:
		if(!PMTel_MsgPrintWait(man)) return FALSE;
		if(wk->seq == 0xFF){
			if(Snd_MePlayCheckBgmPlay()) return FALSE;
		}
		MSGMAN_Delete(man->m_person);
		return TRUE;
	}
	++wk->seq;
	return FALSE;
}
